package com.pti.usermanager.service.impl;

import com.pti.usermanager.dao.api.MenuDao;
import com.pti.usermanager.dao.po.MenuPo;
import com.pti.usermanager.enums.ConstantCode;
import com.pti.usermanager.service.api.MenuService;
import com.pti.usermanager.web.dto.request.SaveMenuRequest;
import com.pti.usermanager.web.dto.request.UpdateMenuRequest;
import com.pti.usermanager.web.dto.response.ResultResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: AbstractResultDto
 * @Author: liuyueyang
 * @Description: 菜单业务实现层
 * @Date: 2019/1/18
 */
@Service
public class MenuServiceImpl implements MenuService{


    private static final Logger logger = LogManager.getLogger(MenuServiceImpl.class);

    @Autowired
    private MenuDao menuDao;

    @Override
    public ResultResponse findTreeByMenu() {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(menuDao.findTreeByMenu());
        return resultResponse;
    }

    @Override
    public ResultResponse findAllByMenuId(Long menuId) {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(menuDao.findAllByMenuId(menuId));
        return resultResponse;
    }

    @Override
    public ResultResponse findAll(String menuNm) {

    	ResultResponse resultResponse = new ResultResponse();
    	List<MenuPo> findAllMenu = menuDao.findAllMenu(menuNm);
    	resultResponse.setData(findAllMenu);
        return resultResponse;
    }

    @Override
    public ResultResponse saveMenu(SaveMenuRequest saveMenuRequest) {
        logger.debug("saveUserRequest:{}",saveMenuRequest.toString());
        ResultResponse resultResponse = new ResultResponse();
        try {
            menuDao.saveMenu(saveMenuRequest);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    @Override
    public ResultResponse updateMenu(UpdateMenuRequest updateMenuRequest) {
        logger.debug("updateUserRequest:{}",updateMenuRequest.toString());

        ResultResponse resultResponse = new ResultResponse();
        try {
            menuDao.updateMenu(updateMenuRequest);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    @Override
    public ResultResponse delMenu(Long menuId) {
        logger.debug("menuId:{}",menuId);

        ResultResponse resultResponse = new ResultResponse();

        try{
            menuDao.delMenu(menuId);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    @Override
    public ResultResponse findAllByUsrId(Long usrId) {
        logger.debug("usrId:{}",usrId);
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(menuDao.findAllByUsrId(usrId));
        return resultResponse;
    }
}
